package com.lbd.project.controller;

import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.project.bean.PaybackPlan;
import com.lbd.project.bean.PersonalPaybackPlan;
import com.lbd.project.controller.vo.TargetVo;
import com.lbd.project.service.PaybackPlanService;
import com.lbd.project.service.PersonalPaybackPlanService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by wang on 2016/9/28.
 */
@RestController
@RequestMapping("/paybackPlan")
public class PaybackPlanController {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    PaybackPlanService paybackPlanService;
    @Autowired
    PersonalPaybackPlanService personalPaybackPlanService;

    /**
     * 还款计划列表
     *
     * @param page        页码
     * @param rows        行数
     * @param paybackPlan 还款计划
     * @return 还款计划列表
     */
    @RequestMapping(value = "/listByTargetId", method = RequestMethod.GET)
    public Result listByTargetId(int page, int rows, PaybackPlan paybackPlan) {
        Page<PaybackPlan> paybackPlanPage = paybackPlanService.findByPageConditionJpql(page, rows, paybackPlan);
        if (null != paybackPlanPage) {
            paybackPlanPage.getContent().stream().forEach(plan -> {
                plan.setTotalAmount(new BigDecimal(plan.getLx() == null ? "0" : plan.getLx())
                        .add(new BigDecimal(plan.getBj() == null ? "0" : plan.getBj())).toString());
                Date yhkr = plan.getYhkr();
                Calendar calendar = Calendar.getInstance();
                if (null != yhkr) {
                    calendar.setTime(yhkr);
                    calendar.set(Calendar.DATE, calendar.get(Calendar.DATE));
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    plan.setYhkr(calendar.getTime());
                }
            });
        }
        return new Result(ResultState.SUCCESS, "查询成功", paybackPlanPage);
    }

    /**
     * 个人还款计划-还款明细
     *
     * @param page
     * @param rows
     * @param personalPaybackPlan
     * @return
     */
    @RequestMapping(value = "/listPersonPayBackPlan", method = RequestMethod.GET)
    public Result listPersonPayBackPlan(int page, int rows, String type, PersonalPaybackPlan personalPaybackPlan) {
        return new Result(ResultState.SUCCESS, "查询成功", personalPaybackPlanService.listByTargetId(page, rows, type, personalPaybackPlan));
    }

    /**
     * 还款中的项目
     *
     * @param page     页码
     * @param rows     行数
     * @param targetVo 筛选条件
     * @return 还款中的项目
     */
    @RequestMapping(value = "/payBackList", method = RequestMethod.GET)
    public Result payBackList(int page, int rows, TargetVo targetVo) {
        return new Result(ResultState.SUCCESS, "查询成功", paybackPlanService.findPayBackList(page, rows, targetVo));
    }

    /**
     * 还款完成的项目
     *
     * @param page     页码
     * @param rows     行数
     * @param targetVo 筛选条件
     * @return 还款完成的项目
     */
    @RequestMapping(value = "/payBackFinish", method = RequestMethod.GET)
    public Result payBackFinish(int page, int rows, TargetVo targetVo) {
        return new Result(ResultState.SUCCESS, "查询成功", paybackPlanService.findPayBackFinishList(page, rows, targetVo));
    }

}
